﻿// 2. Write a program to convert binary numbers to their decimal representation.

using System;

class BinaryToDecimalNumbers
{
    static void Main()
    {

        Console.Write("Enter your binary number : ");
        string binaryNumber = Console.ReadLine();

        int decimalNumber = ConvertIntoDecimal(binaryNumber);

        Console.WriteLine("The decimal representation of the binary number {0} is {1}.", binaryNumber, decimalNumber);
    }

    static int ConvertIntoDecimal(string binaryNumber)
    {
        int decimalNumber = 0;
        int length = binaryNumber.Length;
        int power = 0;

        for (int i = length-1; i >= 0; i--)
        {
            int digit;

            if (binaryNumber[i] == '1')
            {
                digit = 1;
            }
            else
            {
                digit = 0;
            }

            decimalNumber += (int)(digit * Math.Pow(2, power));
            power++;
        }

        return decimalNumber;
    }
}
